﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

/**
 * 创建时间：2014/11/8 11:14:02
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace Assistant
{
    public sealed class ProgressIndicatorHelper
    {
        private PhoneApplicationPage currentPage;
        public ProgressIndicatorHelper(PhoneApplicationPage phoneApplicationPage)
        {
            this.currentPage = phoneApplicationPage;
        }

        public void StartProgressIndicator(ProgressIndicator progressIndicator, string information)
        {
            try
            {
                //加上后提示信息就有问题了
                // this.Dispatcher.BeginInvoke(() =>
                //{
                progressIndicator.IsVisible = true;
                progressIndicator.Text = information;
                progressIndicator.IsIndeterminate = true;
                this.currentPage.SetValue(SystemTray.ProgressIndicatorProperty, progressIndicator);
                this.currentPage.SetValue(SystemTray.OpacityProperty, 0.9);
                //this.currentPage.SetValue(SystemTray.BackgroundColorProperty, Colors.White);
                //this.currentPage.SetValue(SystemTray.ForegroundColorProperty, Helper.HexToColor("#ffc1dc23"));//Colors.Black);      
                this.currentPage.SetValue(SystemTray.BackgroundColorProperty, Helper.HexToColor("#ffff8c00"));
                this.currentPage.SetValue(SystemTray.ForegroundColorProperty, Colors.White);//Colors.Black);    
                SystemTray.IsVisible = true;
                //});
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
            }
        }

        public void StopProgressIndicator(ProgressIndicator progressIndicator, int delayTime = 0)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    Thread.Sleep(delayTime);
                    progressIndicator.IsVisible = false;
                    this.currentPage.ClearValue(SystemTray.ProgressIndicatorProperty);
                    this.currentPage.ClearValue(SystemTray.OpacityProperty);
                    this.currentPage.ClearValue(SystemTray.BackgroundColorProperty);
                    this.currentPage.ClearValue(SystemTray.ForegroundColorProperty);
                    SystemTray.IsVisible = false;
                }
                catch (Exception err)
                {
                    LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
                }
            });

        }
    }
}